# -*- coding: utf-8 -*-
#  递归产生树
# @Time    ： 2021/12/1  23:06
# @Auther  ： 徐良
# File     :  makeTree.py
from share.dataFactory import DataFactory
from PySide6.QtWidgets import QTreeWidgetItem, QAbstractItemView
from PySide6.QtCore import Qt
from PySide6.QtGui import QStandardItemModel, QStandardItem


class MakeTree:
    """
    造树类
    """
    def __init__(self, sSql, tree, baseDbType):
        super(MakeTree, self).__init__()
        self.sql = sSql
        self.tree = tree
        self.baseDbType = baseDbType
        self.__getModel()

    def __getModel(self):
        self.__model = DataFactory.getQueryModelForSql(self.sql, self.baseDbType)

    def createTree(self):
        """
        设置最普通的代码-名称树
        :return:
        """
        # 设置表头
        self.tree.setHeaderLabels(['编码', '名称'])
        data = DataFactory.getQueryModelForSql(self.sql, self.baseDbType)

        itemRoot = self.tree.invisibleRootItem()
        rows = data.rowCount()
        for irow in range(0, rows):
            # 处理第1级
            if data.record(irow).value('flevel') == 1:
                item1 = QTreeWidgetItem(itemRoot)
                item1.setFlags(Qt.NoItemFlags)  # 移除所有标志位，包括选择框
                item1.setText(0, data.record(irow).value('fnumber'))
                item1.setText(1, data.record(irow).value('fname'))

            if data.record(irow).value('flevel') == 2:
                item2 = QTreeWidgetItem(item1)
                item2.setFlags(Qt.NoItemFlags)  # 移除所有标志位，包括选择框
                item2.setText(0, data.record(irow).value('fnumber'))
                item2.setText(1, data.record(irow).value('fname'))

            if data.record(irow).value('flevel') == 3:
                item3 = QTreeWidgetItem(item2)
                item3.setFlags(Qt.NoItemFlags)  # 移除所有标志位，包括选择框
                item3.setText(0, data.record(irow).value('fnumber'))
                item3.setText(1, data.record(irow).value('fname'))

            if data.record(irow).value('flevel') == 4:
                item4 = QTreeWidgetItem(item3)
                item4.setFlags(Qt.NoItemFlags)  # 移除所有标志位，包括选择框
                item4.setText(0, data.record(irow).value('fnumber'))
                item4.setText(1, data.record(irow).value('fname'))

            if data.record(irow).value('flevel') == 5:
                item5 = QTreeWidgetItem(item4)
                item5.setFlags(Qt.NoItemFlags)  # 移除所有标志位，包括选择框
                item5.setText(0, data.record(irow).value('fnumber'))
                item5.setText(1, data.record(irow).value('fname'))

            if data.record(irow).value('flevel') == 6:
                item6 = QTreeWidgetItem(item5)
                item6.setFlags(Qt.NoItemFlags)  # 移除所有标志位，包括选择框
                item6.setText(0, data.record(irow).value('fnumber'))
                item6.setText(1, data.record(irow).value('fname'))

            if data.record(irow).value('flevel') == 7:
                item7 = QTreeWidgetItem(item6)
                item7.setFlags(Qt.NoItemFlags)  # 移除所有标志位，包括选择框
                item7.setText(0, data.record(irow).value('fnumber'))
                item7.setText(1, data.record(irow).value('fname'))

            if data.record(irow).value('flevel') == 8:
                item8 = QTreeWidgetItem(item7)
                item8.setFlags(Qt.NoItemFlags)  # 移除所有标志位，包括选择框
                item8.setText(0, data.record(irow).value('fnumber'))
                item8.setText(1, data.record(irow).value('fname'))
